import * as THREE from "three";
// 导入轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader";
import gsap from "gsap";
import World from "@/three/World.js";

const viewPosition = {
  default: new THREE.Vector3(1, -1, 1).normalize(),
  active: new THREE.Vector3(81.8107, -68.4092, 90.8815).normalize()
};

const viewScalar = {
  Loading: 1000,
  Ready: 90,
  Active: 30
};

export default class ThreePlus {
  constructor(selector) {
    this.view = {
      position: viewPosition.default.clone(),
      scalar: viewScalar.Ready
    };
    this.mixers = [];
    this.clock = new THREE.Clock();
    this.domElement = document.querySelector(selector);
    this.width = this.domElement.clientWidth;
    this.height = this.domElement.clientHeight;
    this.init();
  }

  init() {
    this.initScene();
    this.initCamera();
    this.initRenderer();
    this.initControl();
    this.setWorld();
    this.render();
  }

  initScene() {
    this.scene = new THREE.Scene();
  }

  initCamera() {
    this.camera = new THREE.PerspectiveCamera(
      40,
      this.width / this.height,
      0.001,
      1000
    );
    this.camera.position.copy(this.view.position.clone().multiplyScalar(this.view.scalar));
    this.camera.up.set(0, 0, 1);
    this.camera.aspect = this.width / this.height;
    this.camera.updateProjectionMatrix();
  }

  initRenderer() {
    this.renderer = new THREE.WebGLRenderer({
      // logarithmicDepthBuffer: true,
      antialias: true,
      // alpha: true
    });
    this.renderer.setSize(this.width, this.height);
    this.renderer.shadowMap.enabled = true;
    this.renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
    this.domElement.appendChild(this.renderer.domElement);
  }

  initControl() {
    this.control = new OrbitControls(this.camera, this.renderer.domElement);
  }

  render() {
    let deltaTime = this.clock.getDelta();
    let estimatedTime = this.clock.getElapsedTime();
    for (let i = 0; i < this.mixers.length; i++) {
      this.mixers[i].update(deltaTime * 0.2);
    }
    this.control.update();
    this.world.railCar.update();
    this.world.windMill.update();
    this.world.ship.update();
    this.world.carousel.update();
    this.world.dropRotation.update();
    this.world.dropUp.update();
    this.world.ferris.update();
    this.world.coffeeSmoke.update(estimatedTime);
    this.world.physics.world.fixedStep();
    // this.world.cannonDebugger.update();
    if (this.world.isReady) {
      this.world.car.update();
      this.world.bricks.update();
    }
    this.renderer.render(this.scene, this.camera);
    requestAnimationFrame(this.render.bind(this));
  }

  setLight() {
    // 添加环境光
    this.ambientLight = new THREE.AmbientLight(0xffffff, 1);
    this.scene.add(this.ambientLight);
  }

  addAxes() {
    //添加辅助坐标轴
    let axes = new THREE.AxesHelper(1000);
    this.scene.add(axes);
  }

  setWorld() {
    this.world = new World(this);
  }

}